using UnityEngine;
using System.Collections;

public class Turret : MonoBehaviour {
	
	// public variables
	public GameObject turretBaseObject;
	public GameObject turretRotationObject;
	public GameObject turretElevationObject;
	public GameObject turretBarrelsObject;
	public GameObject myTarget;
	public Quaternion targetTurretRotation;
	public GameObject fireFromHere;
	public GameObject fireParticle;
	public TurretManager myBoss;
	private string turretBehaviour = "ClosestEnemy";
	
	
	public bool shooting = true;
	private Vector3 turretRotation = new Vector3();
	
	public float turretRange = 3;
	public float bulletSpeed = 10;
	
	public GameObject bulletToShoot;
	
	// private variables
	private Vector3 clampHorizontal = new Vector3(0,1,1);
	private Vector3 clampVertical = new Vector3(1,0,1);
	
	// Use this for initialization
	void Start () {
		//addTarget(GameObject.Find("PathCalculator"));
	}
	
	// ADD AND REMOVE TARGET
	public void addTarget(GameObject target){
		myTarget = target;
	}
	
	public void removeTarget(){
		myTarget = null;
	}
	
	// FIRE BULLET
	private void fireABullet(){
		GameObject bulletClone;
		
		bulletClone = (GameObject) Instantiate(bulletToShoot, fireParticle.transform.position, fireParticle.transform.rotation);
		bulletClone.transform.rotation = getTurretAngle();
		//bulletClone.tranform.rotation.x = turretElevationObject.transform.rotation.x;
		//bulletClone.GetComponent(bulletScript).startPosition = fireParticle.transform.position;
		bulletClone.rigidbody.velocity = bulletClone.transform.TransformDirection(Vector3.forward * bulletSpeed);
	}
	
	public Vector3 calculateFirePoint(Vector3 startPoint){
		Vector3 globalPoint = transform.TransformPoint(startPoint);
		Vector3 localPoint = transform.InverseTransformPoint(globalPoint);
		return localPoint;
	}
	
	public Quaternion combineRotationElevationVectors(Quaternion rotQua, Quaternion eleQua){
		Vector3 rotVec = rotQua.eulerAngles;
		Vector3 eleVec = eleQua.eulerAngles;
		Vector3 resultVec = new Vector3();
		resultVec.x = eleVec.x;
		resultVec.y = rotVec.y;
		resultVec.z = 0;
		Quaternion returnQua = Quaternion.Euler(resultVec);
		return returnQua;
	}
	
	public void getNewTarget(){
		print("targetting");
		switch(turretBehaviour){
		case "FirstEnemy":
			print("TARGET FOUND!!!!!!!!!!!");
			addTarget(myBoss.getNewTarget(transform.position,turretRange));
			break;
		case "ClosestEnemy":
			addTarget(myBoss.getNewClosestTarget(transform.position,turretRange));
			break;
		}
	}
	
	public Quaternion getTurretAngle(){
		return combineRotationElevationVectors(turretRotationObject.transform.rotation, turretElevationObject.transform.rotation);
	}
	
	// Update is called once per frame
	void Update () {
		
		fireParticle.transform.position = calculateFirePoint(fireFromHere.transform.position);
		
		if(myTarget != null){
			
			Vector3 turretPositionDistanceCheck = transform.position;
			Vector3 targetPositionDistanceCheck = myTarget.transform.position;
			
			if(Vector3.Distance(turretPositionDistanceCheck, targetPositionDistanceCheck) < turretRange){
				
				// *** TURRET ROTATION ***
				float horizontalSeekSpeed = 1;
				float verticalSeekSpeed = 1;
				
				
				// WORK OUT TURRET DESIRED TURRET ROTATION
				Quaternion currentTurretRotation = turretRotationObject.transform.localRotation;
				turretRotationObject.transform.LookAt(myTarget.transform.position);
				
				targetTurretRotation = turretRotationObject.transform.localRotation;
				turretRotationObject.transform.localRotation = currentTurretRotation;
				
				// CLAMP TURRET ROTATION TO Y AXIS ONLY
				currentTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, currentTurretRotation.eulerAngles);
				targetTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, targetTurretRotation.eulerAngles);
				
				// ROTATE TURRET OVER TIME
				turretRotationObject.transform.localRotation = Quaternion.RotateTowards(currentTurretRotation, targetTurretRotation,horizontalSeekSpeed);
				
				
				// *** BARREL ROTATION ***
				Quaternion quaternionValue;
				
				if(shooting && currentTurretRotation == targetTurretRotation){
					turretRotation.z += 20;
					quaternionValue = Quaternion.Euler(turretRotation);
					turretBarrelsObject.transform.localRotation = quaternionValue;
					fireABullet();
					//fireParticle.active = true;
					fireParticle.particleEmitter.emit = true;
				} else {
					turretRotation.z = 0;
					quaternionValue = Quaternion.Euler(turretRotation);
					fireParticle.particleEmitter.emit = false;
				}
				
				// *** TURRET ELEVATION ***
				Quaternion currentBarrelRotation;
				Quaternion targetBarrelRotation;
				
				switch(2){
					case 1:
						// ARK FIREING SOLUTION
						break;
					case 2:
						currentBarrelRotation = turretElevationObject.transform.localRotation;
						turretElevationObject.transform.LookAt(myTarget.transform.position);
						targetBarrelRotation = turretElevationObject.transform.localRotation;
						turretElevationObject.transform.localRotation = currentBarrelRotation;
						
						//verticalFiringSolution = targetBarrelRotation;
						
						// CLAMP TURRET ROTATION ON THE X AXIS
						currentBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, currentBarrelRotation.eulerAngles);
						targetBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, targetBarrelRotation.eulerAngles);
						
						// ROTATE TURRET OVER TIME
						turretElevationObject.transform.localRotation = Quaternion.RotateTowards(currentBarrelRotation, targetBarrelRotation, verticalSeekSpeed);
						break;
				}
				
				//fireParticle.transform.rotation = combineRotationElevationVectors(turretRotationObject.transform.rotation.eulerAngles, turretElevationObject.transform.rotation.eulerAngles);
				fireParticle.transform.rotation = combineRotationElevationVectors(turretRotationObject.transform.rotation, turretElevationObject.transform.rotation);
				//Vector3 bob = fireParticle.transform.rotation.eulerAngles;
				//transform.rotation = Quaternion.Euler(bob.x,bob.y,bob.z);
				//print(bob.Normalize);
				
			} else {
				fireParticle.particleEmitter.emit = false;
				removeTarget();
			}
		} else {
			getNewTarget();
		}
	}
}
